<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.14/vue.js"></script>
</head>
<body>
    <div id="app">
        <input v-model="username" @input="handleInput"/>
    </div>
</body>
<script>
let vm = new Vue({
    el: '#app',
    data(){
        return {
            username: 'zs',
            list: [{id: 1}, {id:2}]
        }
    },
    methods: {
        //通过v-bind监听事件触发
        handleInput(e){
            // console.log(e.target.value)
        }
    },
    // 通过watch监听数据改变
    watch: {
        //简化写法
        /*
        username(val){
            console.log('val', val)
        }
        */
        // 对象写法
        username: {
            handler(val){
                console.log('val', val)
            },
            immediate: true //立即监听
        },
        /*
        list(val){
            console.log('val', val)
        }*/
        list: {
            handler(val){
                console.log(val)
            },
            deep: true
        } 
    }
})

// vm.list.push({id: 3}) // 触发侦听器简化写法
// vm.list.splice(0, 1) // 触发侦听器简化写法
vm.list[0].id = 100 //不会触发侦听器简化写法,需要deep: true
</script>
</html>